package leetcode_top;

public class Ex162 {
    class Solution {
        public int findPeakElement(int[] nums) {
            if (nums == null || nums.length == 0) return -1;
            else if (nums.length == 1) return 0;
            int lo = 0, hi = nums.length - 1;
            if (nums[lo] > nums[lo + 1]) return lo;
            lo++;
            if (nums[hi] > nums[hi - 1]) return hi;
            hi--;
            int mid;
            while (lo < hi) { //大的一侧才有峰值
                //某一个时刻，当lo与hi相邻时，较大者就是峰值
                mid = (hi - lo) / 2 + lo;
                if (nums[mid] >= nums[mid + 1]) {
                    hi = mid;
                } else {
                    lo = mid + 1;
                }
            }
            return lo;
        }
    }
}
